package com.amigo.online.provider.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.base.security.LocalUser;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.Result;
import com.amigo.online.common.core.security.IgnoreSecurity;
import com.amigo.online.common.param.dto.vip.PayVipDto;
import com.amigo.online.common.param.result.BlackUserInfo;
import com.amigo.online.common.param.result.VipChatInfo;
import com.amigo.online.common.param.result.VipInfo;
import com.amigo.online.common.param.result.VipPriceInfo;
import com.amigo.online.provider.user.mapper.VipChatMapper;
import com.amigo.online.provider.user.mapper.VipMemberTblMapper;
import com.amigo.online.provider.user.mapper.VipPriceTblMapper;
import com.amigo.online.provider.user.model.entity.VipChat;
import com.amigo.online.provider.user.model.entity.VipMemberTbl;
import com.amigo.online.provider.user.model.entity.VipPriceTbl;
import com.amigo.online.provider.user.service.BlackService;
import com.amigo.online.provider.user.service.VIPService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * VIP
 *
 * @author amigo
 */
@Api("VIP")
@RestController
@RequestMapping(value = "/vip", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Slf4j
public class VIPController extends BaseController {
    @Autowired
    VIPService vipService;
    @Autowired
    VipPriceTblMapper vipPriceTblMapper;
    @Autowired
    VipChatMapper vipChatMapper;


    @Autowired
    VipMemberTblMapper vipMemberTblMapper;

    @ApiOperation(value = "获取用户Vip信息" ,  notes="获取Vip信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id",paramType = "path",required = true)
    })
    @GetMapping("vip/{userId}")
    @IgnoreSecurity
    public Result<VipInfo> getVip(@PathVariable long userId){
        return Result.success(vipService.getVipInfo(userId));
    }


    @ApiOperation(value = "购买VIP" ,  notes="购买VIP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @PostMapping("payVip")
    public Result payVip(@RequestBody PayVipDto payVipDto){
        vipService.payVip(payVipDto);
        return Result.success();
    }
    @ApiOperation(value = "IOS购买VIP" ,  notes="IOS购买VIP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @PostMapping("payVipByIOS")
    public Result payVipByIOS(@RequestBody JSONObject jsonObject){
        vipService.payVipByIOS(jsonObject.getString("base64"));
        return Result.success();
    }

    @ApiOperation(value = "聊天" ,  notes="聊天")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
            @ApiImplicitParam(name = "userId", value = "接收消息的人的ID",paramType = "header",required = true),
    })
    @PostMapping("vipChat/{userId}")
    public Result vipChat(Long userId){
        return Result.success(vipService.vipChat(userId));

    }
    @ApiOperation(value = "查询会员充值费用" ,  notes="查询会员充值费用")
    @IgnoreSecurity
    @GetMapping("price")
    public Result<List<VipPriceInfo>> getPrice(){
            return Result.success(price());
    }
    private List<VipPriceInfo> price(){
        List<VipPriceInfo> vipPriceInfos = new ArrayList<>();
        List<VipPriceTbl> list = vipPriceTblMapper.selectAll();
        Locale locale = LocaleContextHolder.getLocale();
        String language = locale.getLanguage();
        for (VipPriceTbl vipPriceTbl : list){
            VipPriceInfo vipPriceInfo = JSON.parseObject(JSONObject.toJSONString(vipPriceTbl),VipPriceInfo.class);
            if("zh".equals(language)){//中文
                vipPriceInfo.setName(vipPriceTbl.getNameCn());
//                systemMessageInfo.setTitle(messagePushTbl.getTitleCn());
            }else if("en".equals(language)){//英语
                vipPriceInfo.setName(vipPriceTbl.getNameEn());
//                systemMessageInfo.setTitle(messagePushTbl.getTitleEn());
            }else if("pt".equals(language)){//葡萄牙
                vipPriceInfo.setName(vipPriceTbl.getNamePt());
//                systemMessageInfo.setTitle(messagePushTbl.getTitlePt());
            }else if("es".equals(language)){//葡萄牙
                vipPriceInfo.setName(vipPriceTbl.getNameEs());
//                systemMessageInfo.setTitle(messagePushTbl.getTitleEs());
            }else {
                vipPriceInfo.setName(vipPriceTbl.getNameEn());
//                systemMessageInfo.setTitle(messagePushTbl.getTitleEn());
            }
            vipPriceInfos.add(vipPriceInfo);
        }
        return vipPriceInfos;
    }
}
